package com.xbongbong.parent.abstracts;

import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.interfaces.BusinessConditionService;
import com.xbongbong.parent.pojo.list.ConditionListPojo;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.toolbox.help.SaasListDataPermissionHelp;
import com.xbongbong.sys.model.UserModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/06/15 16:35
 */
@Component
public abstract class AbstractConditionService implements BusinessConditionService {

    @Resource
    private UserModel userModel;
    @Resource
    private SaasListDataPermissionHelp saasListDataPermissionHelp;

    /**
     * ->com.xbongbong.saas.service.impl.SaasListServiceImpl.handleConditionBySubBusinessType
     * @param conditionListPojo
     * @return
     */
    @Override
    public List<ConditionsEntityExt> handleConditionBySubBusinessType(ConditionListPojo conditionListPojo) {
        if (Objects.equals(conditionListPojo.getStatisticsType(), StatisticsTypeEnum.CUSTOM_STATISTIC.getCode())) {
            return new ArrayList<>();
        }
        if (Objects.isNull(conditionListPojo.getSubBusinessType())){
            return new ArrayList<>();
        }
        return packageConditionBySubBusinessType(conditionListPojo);
    }

    @Override
    public void dingtalkRangeScreen(List<ConditionsEntityExt> conditionsList, UserVO userVO, Integer businessType, Integer subBusinessType, String platform) {
        if (!Objects.equals(StringConstant.PLATFORM_DINGTALK, platform)) {
            return;
        }
        if (Objects.isNull(conditionsList) || conditionsList.isEmpty()) {
            return;
        }

        SaasListDataPermissionHelp.dealRange(conditionsList);
        if (Objects.equals(subBusinessType, XbbRefTypeEnum.CUSTOMER_RECYCLE.getCode()) || Objects.equals(subBusinessType,XbbRefTypeEnum.CLUE_RECYCLE.getCode())){
            return;
        }

        Set<String> appRangeAttrList = getAppRangeAttrList(businessType);
        if (CollectionsUtil.isEmpty(appRangeAttrList)) {
            return;
        }
        saasListDataPermissionHelp.removeByPermission(conditionsList, userVO, appRangeAttrList);
    }

    protected Set<String> getAppRangeAttrList(Integer businessType) {
        return SaasListDataPermissionHelp.getAppRangeAttrList(businessType);
    }
}
